<?php
	include_once("config.php");
	
	/**
	 * This function authenticate the user into the Labora Scheduler system
	 */
	function authentication(){
		return true;
	}
	
	/**
	 * This function create and return a crypt password based in a md5 salt.
	 * If param salt is empty, the function will create a random salt.
	 * @param $password String containing the password to be crypted.
	 * @param $salt String containing the md5 salt to crypt the password.
	 */
	function md5crypt($password, $salt=NULL){
		if(!$salt){
			// create a salt that ensures crypt creates an md5 hash
			$base64_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
							 . 'abcdefghijklmnopqrstuvwxyz0123456789+/';
			$salt = '$1$';
			
			for($i=0; $i<8; $i++)
				$salt .= $base64_alphabet[rand(0,63)];
			
			$salt .= '$';
		}
		
		return "{CRYPT}" . crypt($password, $salt) . "";
	}
	
	/**
	 * This function filters the received list and return only the specified elements that is contained in $filter.
	 * @param $filter Array containing the ids to be returned in the list.
	 * @param $list Array containing the list to be filtered.
	 * @return Array containing only the specified elements that is contained in $filter.
	 */
	function filter_LS_list($filter, $list){
		$filtered_list = array();

		for($i = 0; $i < count($list); $i++){
			if(in_array($list[$i]['id'], $filter))
				$filtered_list[] = $list[$i];
		}
		
		return $filtered_list;
	}
	
	/**
	 * This function verify the parameters has been correctly sent.
	 * @param $received_paramenters Array containing the received parameters.
	 * @param $obligatory_parameters Array containing the obligatory parameters and each types.
	 * @param $optional_parameters Array containing the optional parameters and each types.
	 * @return Array containing the parameters, if the received parameters contains all obligatory parameters or Boolean false when not.
	 */
	function verify_paramenters($received_paramenters, $obligatory_parameters, $optional_parameters){
		$parameters = array(null);
		$all_parameters = array_merge($obligatory_parameters, $optional_parameters);
		
		$received_op = 0;
		
		for($i=0; $i < count($received_paramenters); $i++){
			$rp_key_name = array_keys($received_paramenters)[$i];
			
			for($j = 0; $j < count($all_parameters); $j++){
				$ap_key_name = array_keys($all_parameters)[$j];
				
				if($rp_key_name == $ap_key_name && $ap_key_name != NULL){
					switch($all_parameters[''.$ap_key_name.'']){
						case 'numeric':
							if(!is_numeric($received_paramenters[''.$rp_key_name.'']))
								break 2;
						break;
						case 'array':
							$decoded_array = json_decode($received_paramenters[''.$rp_key_name.''], true);
							if(!is_array($decoded_array))
								break 2;
						break;
					}
					
					if($all_parameters[''.$ap_key_name.''] == 'array')
						$parameters[''.$rp_key_name.''] = $decoded_array;
					else
						$parameters[''.$rp_key_name.''] = $received_paramenters[''.$rp_key_name.''];
					
					if(in_array($rp_key_name, array_keys($obligatory_parameters), true))
						$received_op++;
				}
			}
		}
		
		/* if we have parameters, remove the null key */
		if(count($parameters) > 1 && ($key = array_search(null, $parameters)) !== false) {
			unset($parameters[$key]);
		}
		
		$count_op = count($obligatory_parameters);
		if(($key = array_search(null, $obligatory_parameters)) !== false) {
			$count_op--;
		}

		if($received_op == $count_op)
			return $parameters;
		else
			return FALSE;
	}

	/**
	 * This function connect to the Labora Scheduler database
	 * If the caller not pass the parameters, the function will use the default connection parameters
	 * @param $host String containing the host of database
	 * @param $database String containing the database name
	 * @param $user String containing the user to authenticate in the database
	 * @param $password String containing the password to authenticate in the database
	 * @param $port Integer containing the port of database server (Default in the Postgres: 5432)
	 */
	function database_connect($host=DATABASE_HOSTNAME, $database=DATABASE_NAME, $user=DATABASE_USERNAME, $password=DATABASE_PASSWORD, $port=DATABASE_PORT){
		$connection_string = "host=".$host." port=".$port." dbname=".$database." user=".$user." password=".$password."";
		$connect = pg_connect($connection_string);
		
		return $connect;
	}

	/**
	 * This function execute a query in the database and return the result
	 * @param $database_connection Connection of database
	 * @param $query String containing the query to be executed
	 */
	function database_query($database_connection, $query){
		$query_result = pg_query($database_connection, $query);
		
		return $query_result;
	}

	/**
	 * This function return a row (record) as an object
	 * @param $query_result Query result obtained by the function database_query()
	 * @param $row Integer containing the position of record to be returned (first row = 0)
	 */
	function database_fetch_object($query_result, $row=0){
		$fetch_object = pg_fetch_object($query_result, $row);
		
		return $fetch_object;
	}

	/**
	 * This function return a row (record) as an array
	 * @param $query_result Query result obtained by the function database_query()
	 * @param $row Integer containing the position of record to be returned (first row = 0)
	 */
	function database_fetch_array($query_result, $row=0){
		$fetch_array = pg_fetch_array($query_result, $row, PGSQL_ASSOC);
		
		return $fetch_array;
	}
	
	/**
	 * This function return the integer informing the amount of rows in the query
	 * @param $query_result Query result obtained by the function database_query()
	 */
	function database_num_rows($query_result){
		$num_rows = pg_num_rows($query_result);
		
		return $num_rows;
	}

	/**
	 * This function close the connection created by the function database_connect()
	 * @param $database_connection Database connection to be closed
	 */
	function database_close($database_connection){
		$close_connection = pg_close($database_connection);
		
		return $close_connection;
	}
?>
